package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserInfoServer userInfoServer;

    /**
     * 保存用户信息
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,
                                       @RequestHeader("Authorization") String token) {
        /*//2.向userinfo中设置id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/
        userInfo.setId(UserHolder.getUserId());
        //3.调用server
        userInfoServer.save(userInfo);

        //4.响应
        return ResponseEntity.ok(null);
    }

    /**
     * 上传用户头像
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
        //已经在拦截器中判断了
       /* //1.判断token是否合法
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken) {
            return ResponseEntity.status(401).body(null);
        }
        //拦截器获取用户id了
        //2.向userinfo中设置id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/
        //3.调用service
        userInfoServer.updateHead(headPhoto,UserHolder.getUserId());
        //4.响应
        return ResponseEntity.ok(null);
    }

}
